package io.afu.baseframework.utils;

public class UrlUtils {
    /**
     * 获取URL的协议
     * @param url
     * @return 协议如：http https tcp ftp 等
     */
    public static String getSchema(String url){
        if (url.contains("http://") && url.startsWith("http://")){
            return "http";
        }
        if (url.contains("https://") && url.startsWith("https://")){
            return "https";
        }
        return null;
    }

    /**
     * 获取URL里的域名
     * @param url url
     * @return 域名
     */
    public static String getDomain(String url) {
        String withOutSchema = url.replace("http://","")
                .replace("https://","");
        String[] splitUrl = withOutSchema.split("/");
        if (splitUrl.length>0){
            return splitUrl[0];
        }
        return null;
    }

    /**
     * 获取URL中的路径
     * @param url url
     * @return 路径
     */
    public static String getCurrentPath(String url){
        int lastIndex = url.lastIndexOf("/");
        return url.substring(0,lastIndex+1);
    }

    /**
     * 获取URL中的纯路径
     * @param url url
     * @return 不带路径的URL
     */
    public static String getUrlWithoutPath(String url){
        String schema = getSchema(url);
        String domain = getDomain(url);
        return schema+"://"+domain;
    }

    public static void main(String[] args){
        System.out.println(UrlUtils.getCurrentPath("https://blog.csdn.net/zjx2016/article/details/74557301"));
        System.out.println(UrlUtils.getDomain("https://://blog.csdn.net/zjx2016/article/details/74557301"));
    }


}
